import TodoEvent from "../TodoEvent"
import { ITodoData } from "./typing"
import $ from 'jquery'
import IpConfig from "../IpConfig/IpConfig"

//? 装饰器怎么拿到class的实例
export function getTodoList(target: any, name: string, descriptor: PropertyDescriptor) {
    const _original = descriptor.value
    descriptor.value = function (ITodoData: ITodoData) {
        $.get(`${IpConfig.ip}/todolist`).then(res => {
            ITodoData = res
            if (!res) return
            else _original.call(this, ITodoData)
        })
    }
}

export function removeTodo(target: any, name: string, descriptor: PropertyDescriptor) {
    const _original = descriptor.value!
    descriptor.value = function (id: number) {
        const p = { id }
        $.post(`${IpConfig.ip}/remove`, p).then((res: any) => {
            if (res.code !== 200) return
            else _original.call(this, id)
        })
    }
}


export function addTodo(target: any, name: string, descriptor: PropertyDescriptor) {
    const _original = descriptor.value!

    descriptor.value = function (ITodoData: ITodoData) {
        const p = { todo: ITodoData }
        $.post(`${IpConfig.ip}/add`, p).then((res: any) => {
            if (res.code !== 200) return
            else _original.call(this, ITodoData)
        })
    }
}

